﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZoneLine.Models;
using ZoneLine.Models.QOPStation;

namespace ZoneLine.Stations.QOPStation
{

    /// <summary>
    /// 
    /// </summary>
    /// <remarks>
    /// <para>合件压装工位</para>
    /// <para>StationCode：OP610 IP：--</para>
    /// </remarks>
    public class QOP060Station : WorkStationDBase
    {
        public void Init(string ip, int port = 102, string startAddr = "DB55.0", string endUploadCodeAddr = "DB55.18.1", string endUploadDataAddr = "DB55.18.2", string allowWorkAddr = "DB55.20")
        {
            Ip = ip;
            Port = port;
            _startAddr = startAddr;
            EndUpLoadCodeAddr = endUploadCodeAddr;
            EndUpLoadDataAddr = endUploadDataAddr;
            AllowWorkAddr = allowWorkAddr;
            HeartToPlcAddr = "DB1000.80.3";
            StationCode = "OP610";
            _readLength = 86;

            StationRecordData = new QOP060StationResult();
            _logUiChannel = CurrentStationName + "uilog";
        }

        private QOP060StationResult _stationRecordData;

        public QOP060StationResult StationRecordData
        {
            get { return _stationRecordData; }
            set => SetProperty(ref _stationRecordData, value);
        }

        protected override void DealData(byte[] content)
        {
            StationRecordData.AbsScanCode = GetPlcString(content, 12);
            StationRecordData.PartType = GetPlcString(content, 34);
            StationRecordData.Result = _client.ByteTransform.TransInt16(content, 1000).ToString().Equals("1") ? "OK" : "NG";
            StationRecordData.CycleTime = _client.ByteTransform.TransSingle(content, 82);
        }

        protected override void DealDataCommon(byte[] content)
        {
            StationCode = GetPlcString(content, 0);
            Heart = _client.ByteTransform.TransBool(content, 18 * 8);
            StartUploadCode = _client.ByteTransform.TransBool(content, 80 * 8);
            StartUploadData = _client.ByteTransform.TransBool(content, 80 * 8 + 1);

            HeartToPlc = _client.ByteTransform.TransBool(content, 80 * 8 + 3);
            EndUpLoadCode = _client.ByteTransform.TransBool(content, 80 * 8 + 4);
            EndUpLoadData = _client.ByteTransform.TransBool(content, 80 * 8 + 5);
            AllowWork = _client.ByteTransform.TransInt16(content, 20);
        }

        protected override void DealWithSignal(string name)
        {
            if (name == nameof(StartUploadCode))
            {
                //todo 上传了条码 这时候验证是否要工作
                if (StartUploadCode == false)
                {
                    LogWithUi(_logUiChannel, $"在{CurrentStationName}收到上传条码命令变为false", StationRecordData.AbsScanCode);
                    WriteData(EndUpLoadCodeAddr, false, StationRecordData.AbsScanCode);
                    WriteData(AllowWorkAddr, 0, StationRecordData.AbsScanCode);
                    LogWithUi(_logUiChannel, $"在{CurrentStationName}复位工作控制指令和复位上传条码完成指令", StationRecordData.AbsScanCode);
                    return;
                }
                else
                {
                    LogWithUi(_logUiChannel, $"在{CurrentStationName}收到上传条码信息命令", StationRecordData.AbsScanCode);

                    LogWithUi(_logUiChannel, $"在{CurrentStationName}工位结论验证,条码信息是{StationRecordData.AbsScanCode}", StationRecordData.AbsScanCode);
                    var validataRlt = Validata(CurrentStationName, StationRecordData.AbsScanCode);
                    LogWithUi(_logUiChannel, $"在{CurrentStationName}工位的验证结果是{validataRlt}", StationRecordData.AbsScanCode);
                    var writeRlt = WriteWorkActionToPlc(AllowWorkAddr, validataRlt, StationRecordData.AbsScanCode);
                    LogWithUi(_logUiChannel, $"在{CurrentStationName}工位向下位机下发消息，下发{writeRlt}", StationRecordData.AbsScanCode);
                    return;
                }
            }

            if (name == nameof(StartUploadData))
            {
                if (StartUploadData)
                {
                    //todo 上传数据 这时候将数据记录到数据库中
                    LogWithUi(_logUiChannel, $"在{CurrentStationName}收到上传记录数据命令", StationRecordData.AbsScanCode);
                    var rlt = RecordData();
                    WriteData(EndUpLoadDataAddr, rlt == 1, StationRecordData.AbsScanCode);
                    LogWithUi(_logUiChannel, $"在{CurrentStationName}数据记录,rlt={rlt}", StationRecordData.AbsScanCode);
                }
                else
                {
                    LogWithUi(_logUiChannel, $"在{CurrentStationName}收到上传记录数据命令变为false", StationRecordData.AbsScanCode);

                    WriteData(EndUpLoadDataAddr, false);
                    LogWithUi(_logUiChannel, $"在{CurrentStationName}数据记录,clear", StationRecordData.AbsScanCode);
                }
            }
        }

        protected override int RecordData()
        {
            var result = new QOP060StationResult()
            {
                AbsScanCode = StationRecordData.AbsScanCode,
                StationCode = this.StationCode,
                PartType = StationRecordData.PartType,
                Result = StationRecordData.Result,
                CycleTime = StationRecordData.CycleTime,
            };
            var writeRlt = false;
            if (_dal.CheckExist<QOP060StationResult>(p => p.AbsScanCode == result.AbsScanCode))
            {
                var beforeRlt = _dal.GetModel<QOP060StationResult>(p => p.AbsScanCode == result.AbsScanCode);
                result.Id = beforeRlt.Id;
                writeRlt = _dal.UpdateModelSuper<QOP060StationResult>(p => p.AbsScanCode == result.AbsScanCode, result);
            }
            else
            {
                writeRlt = _dal.AddModel(result);
            }
            return writeRlt ? 1 : 2;
        }
    }
}